package ras;

import java.sql.*;
import java.util.ArrayList;

public class UserDao {

    public enum LoginType {
        SUCCESS,
        FAIL,
        ONLINE,
        UNREGISTERED,
        PASSWORDERROR
    }

    public enum RegisterType {
        SUCCESS,
        FAIL,
        REGISTERED
    }

    public LoginType login(String username, String password) {
        UserInfo userInfo = findUser(username);
        if (userInfo == null) {
            return LoginType.UNREGISTERED;
        } else if (userInfo.getStatus() != 0) {
            return LoginType.ONLINE;
        } else if (!userInfo.getPassword().equals(password)) {
            return LoginType.PASSWORDERROR;
        } else if (userInfo.getPassword().equals(password)) {
            return LoginType.SUCCESS;
        }
        return LoginType.FAIL;
    }

    public RegisterType register_(String username, String password) {
        UserInfo userInfo = findUser(username);
        if (userInfo != null) {
            return RegisterType.REGISTERED;
        } else {
            String sql = "insert into userinfo(username,password,status) values (?,?,?)";
            Connection conn = JDBCUtils.getConnection();
            try {
                PreparedStatement pst = conn.prepareStatement(sql);
                pst.setString(1, username);
                pst.setString(2, password);
                pst.setInt(3, 0);
                if (pst.executeUpdate() > 0) {
                    return RegisterType.SUCCESS;
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                JDBCUtils.close(conn);
            }
            return RegisterType.FAIL;
        }


    }

//    public boolean register_(String username, String password) {
//        String sql = "select * from userinfo where username = ?";
//        Connection conn = JDBCUtils.getConnection();
//        try {
//            PreparedStatement pst = conn.prepareStatement(sql);
//            pst.setString(1, username);
//            if (!pst.executeQuery().next()) {
//                sql = "insert into userinfo(username,password) values (?,?)";
//                pst = conn.prepareStatement(sql);
//                pst.setString(1, username);
//                pst.setString(2, password);
//                if (pst.executeUpdate() > 0) {
//                    return true;
//                }
//            }
//        } catch (SQLException e) {
//            e.printStackTrace();
//        } finally {
//            JDBCUtils.close(conn);
//        }
//        return false;
//    }

    public boolean register(UserInfo userInfo) {
        String sql = "insert into userinfo(username,password) values (?,?)";
        Connection conn = JDBCUtils.getConnection();
        try {
            PreparedStatement pst = conn.prepareStatement(sql);
            pst.setString(1, userInfo.getUsername());
            pst.setString(2, userInfo.getPassword());
            if (pst.executeUpdate() > 0) {
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(conn);
        }
        return false;
    }

//    public UserInfo findUser(String userid) {
//        String sql = "select * from userinfo where username = ?";
//        Connection conn = JDBCUtils.getConnection();
//        UserInfo userInfo = null;
//        try {
//            PreparedStatement pst = conn.prepareStatement(sql);
//            pst.setString(1, userid);
//            ResultSet rs = pst.executeQuery();
//            while (rs.next()) {
//                String username = rs.getString(2);
//                String password = rs.getString(3);
//                userInfo = new UserInfo(username, password);
//            }
//        } catch (SQLException e) {
//            e.printStackTrace();
//        } finally {
//            JDBCUtils.close(conn);
//        }
//        return userInfo;
//    }

    public UserInfo findUser(String userid) {
        String sql = "select * from userinfo where username = ?";
        Connection conn = JDBCUtils.getConnection();
        UserInfo userInfo = null;
        try {
            PreparedStatement pst = conn.prepareStatement(sql);
            pst.setString(1, userid);
            ResultSet rs = pst.executeQuery();
            if (rs.next()) {
                String username = rs.getString(2);
                String password = rs.getString(3);
                int status = rs.getInt(4);
                userInfo = new UserInfo(username, password, status);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(conn);
        }
        return userInfo;
    }

    public boolean setUserStatus(String username, int status) {
        String sql = "select * from userinfo where username = ?";
        Connection conn = JDBCUtils.getConnection();
        try {
            PreparedStatement pst = conn.prepareStatement(sql);
            pst.setString(1, username);
            if (pst.executeQuery().next()) {
                sql = "update userinfo set status = ? where username = ?";
                pst = conn.prepareStatement(sql);
                pst.setInt(1, status);
                pst.setString(2, username);
                if (pst.executeUpdate() > 0) {
                    return true;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(conn);
        }
        return false;
    }

    public ArrayList<String> getOnlineUserList() {
        String sql = "select * from userinfo where status = 1";
        ArrayList<String> list = new ArrayList<>();
        Connection conn = JDBCUtils.getConnection();
        try {
            PreparedStatement pst = conn.prepareStatement(sql);
            ResultSet rs = pst.executeQuery();
            while (rs.next()) {
                list.add(rs.getString(2));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(conn);
        }
        return list;
    }
}
